<?php
/**
 * 快递查询接口
 * @author cjli
 *
 */

class Express
{
	var $_name;//快递代号名称
	var $_invoice_sn;
	function __construct($express_name = '')
	{
		if( preg_match('/^[a-zA-Z][a-zA-Z0-9_]$/i', $express_name)){
			die('快递公司代号名称错误');
		}
		$express_name = strtolower($express_name);
		$this->_name = $express_name ? $express_name : 'ems';
		
	}
	
	public function query($invoice_sn)
	{
		$this->_invoice_sn = $invoice_sn;
		switch ($this->_name){
			case 'ems' : return $this->query_ems();
			case 'yto' : return $this->query_yto();
			case 'zto' : return $this->query_zto();
			case 'sto_express' : return $this->query_sto_express();
			case 'sf_express' : return $this->query_sf_express();
			case 'post_mail' : return $this->query_post_mail();
			case 'post_express' : return $this->query_post_express();
			default:return $this->query_ems();
		}
	}
	
	//EMS快递查询
	private function query_ems(){
		$str = '<form style="margin:0px" method="post" '.
    'action="http://www.ems.com.cn/qcgzOutQueryAction.do" name="queryForm_' .$this->_invoice_sn. '" target="_blank">'.
    '<input type="hidden" name="mailNum" value="' .$this->_invoice_sn. '" />'.
    '<a href="javascript:document.forms[\'queryForm_' .$this->_invoice_sn. '\'].submit();">' .$this->_invoice_sn. '</a>'.
    '<input type="hidden" name="reqCode" value="browseBASE" />'.
    '<input type="hidden" name="checknum" value="0568792906411" />'.
    '</form>';
    return $str;
	}
	
	//中通快递
	private function query_zto()
	{
		$str = '<form style="margin:0px" methods="post" '.
            'action="http://www.zto.cn/bill.asp" name="queryForm_' .$this->_invoice_sn. '" target="_blank">'.
            '<input type="hidden" name="ID" value="' .str_replace("<br>","\n",$this->_invoice_sn). '" />'.
            '<a href="javascript:document.forms[\'queryForm_' .$this->_invoice_sn. '\'].submit();">' .$this->_invoice_sn. '</a>'.
            '<input type="hidden" name="imageField.x" value="26" />'.
            '<input type="hidden" name="imageField.x" value="43" />'.
            '</form>';

        return $str;
	}
	
	//圆通快递查询
	private function query_yto()
	{
		//圆通快递查询会判断链接来源，目前的查询无法生效。
        $str = '<form style="margin:0px" methods="post" '.
            'action="http://www.yto.net.cn/service/sql.aspx" name="queryForm_' .$this->_invoice_sn. '" target="_blank">'.
            '<input type="hidden" name="NumberText" value="' .$this->_invoice_sn. '" />'.
            '<a href="javascript:document.forms[\'queryForm_' .$this->_invoice_sn. '\'].submit();">' .$this->_invoice_sn. '</a>'.
            '</form>';

        return $str;
		
	}
	
	//申通快递
	private function query_sto_express()
	{
		$str = '<form style="margin:0px" methods="post" '.
            'action="http://115.238.100.211:8081/result.aspx" name="queryForm_' .$this->_invoice_sn. '" target="_blank">'.
            '<input type="hidden" name="wen" value="' .str_replace("<br>","\n",$this->_invoice_sn). '" />'.
            '<a href="javascript:document.forms[\'queryForm_' .$this->_invoice_sn. '\'].submit();">' .$this->_invoice_sn. '</a>'.
            '</form>';

        return $str;
	}
	
	//顺风快递查询
	private function query_sf_express()
	{
		 $form_str = '<a href="http://www.sf-express.com/tabid/68/Default.aspx" target="_blank">' .$this->_invoice_sn. '</a>';
        return $form_str;
	}
	
	//邮局平邮
	private function query_post_mail()
	{
		 return false;
	}
	
	//邮政包裹
	private function query_post_express()
	{
		 return false;
	}
}